package com.wvmi.gmall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 描述:   <br>
 *
 * @author Wuminming <br>
 * 创建时间: 2023-12-06 15:47 <br>
 * <p>
 * Copyright: 2013 - 2023 www.szwcyq.com Inc. All rights reserved. <br>
 * 注意：本内容仅限于深圳市微创云启科技有限公司内部传阅，禁止外泄以及用于其他的商业目的<br>
 */
@Configuration
public class WareRabbitMQConfig {

    public final static String STOCK_EVENT_EXCHANGE = "stock-event-exchange";
    public final static String STOCK_RELEASE_$_ROUTE_KEY = "stock.release.#";
    public final static String STOCK_RELEASE_ROUTE_KEY = "stock.release";
    public final static String STOCK_LOCKED_ROUTE_KEY = "stock.locked";
    public final static String STOCK_DELAY_QUEUE = "stock.delay.queue";
    public final static String STOCK_RELEASE_STOCK_QUEUE = "stock.release.stock.queue";

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange() {
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(STOCK_EVENT_EXCHANGE, true, false);
    }

    /**
     * 普通队列
     * @return
     */
    @Bean
    public Queue stockReleaseStockQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        return new Queue(STOCK_RELEASE_STOCK_QUEUE, true, false, false);
    }


    /**
     * 延迟队列
     * @return
     */
    @Bean
    public Queue stockDelay() {

        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", STOCK_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", STOCK_RELEASE_ROUTE_KEY);
        // 消息过期时间 2分钟
        arguments.put("x-message-ttl", 120000);

        return new Queue(STOCK_DELAY_QUEUE, true, false, false,arguments);
    }


    /**
     * 交换机与普通队列绑定
     * @return
     */
    @Bean
    public Binding stockLocked() {
        //String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			Map<String, Object> arguments

        return new Binding(STOCK_RELEASE_STOCK_QUEUE,
                Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE,
                STOCK_RELEASE_$_ROUTE_KEY,
                null);
    }


    /**
     * 交换机与延迟队列绑定
     * @return
     */
    @Bean
    public Binding stockLockedBinding() {
        return new Binding(STOCK_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                STOCK_EVENT_EXCHANGE,
                STOCK_LOCKED_ROUTE_KEY,
                null);
    }
}
